#include <FastLED.h>

// 定义LED数量和引脚
#define NUM_LEDS 64
#define DATA_PIN 2
#define BUTTON_PIN 14  // 模式切换按钮

// 创建LED数组
CRGB leds[NUM_LEDS];

// 数字0-9的点阵图案
const uint8_t digits[10][8] = {
  { 0b01110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b01110 }, // 0
  { 0b00100, 0b01100, 0b00100, 0b00100, 0b00100, 0b00100, 0b00100, 0b01110 }, // 1
  { 0b01110, 0b10001, 0b00001, 0b00010, 0b00100, 0b01000, 0b10000, 0b11111 }, // 2
  { 0b01110, 0b10001, 0b00001, 0b00110, 0b00001, 0b00001, 0b10001, 0b01110 }, // 3
  { 0b00010, 0b00110, 0b01010, 0b10010, 0b11111, 0b00010, 0b00010, 0b00010 }, // 4
  { 0b11111, 0b10000, 0b10000, 0b11110, 0b00001, 0b00001, 0b10001, 0b01110 }, // 5
  { 0b01110, 0b10001, 0b10000, 0b11110, 0b10001, 0b10001, 0b10001, 0b01110 }, // 6
  { 0b11111, 0b00001, 0b00010, 0b00100, 0b01000, 0b01000, 0b01000, 0b01000 }, // 7
  { 0b01110, 0b10001, 0b10001, 0b01110, 0b10001, 0b10001, 0b10001, 0b01110 }, // 8
  { 0b01110, 0b10001, 0b10001, 0b01111, 0b00001, 0b00001, 0b10001, 0b01110 }  // 9
};

// 字母A-G的点阵图案
const uint8_t letters[7][8] = {
  { 0b01110, 0b10001, 0b10001, 0b11111, 0b10001, 0b10001, 0b10001, 0b10001 }, // A
  { 0b11110, 0b10001, 0b10001, 0b11110, 0b10001, 0b10001, 0b10001, 0b11110 }, // B
  { 0b01110, 0b10001, 0b10000, 0b10000, 0b10000, 0b10000, 0b10001, 0b01110 }, // C
  { 0b11110, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b10001, 0b11110 }, // D
  { 0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000, 0b11111 }, // E
  { 0b11111, 0b10000, 0b10000, 0b11110, 0b10000, 0b10000, 0b10000, 0b10000 }, // F
  { 0b01110, 0b10000, 0b10000, 0b10000, 0b10111, 0b10001, 0b10001, 0b01110 }  // G
};

// 模式枚举
enum DisplayMode {
  MODE_GREEN_BACKGROUND,
  MODE_RED_BACKGROUND,
  MODE_BLUE_BACKGROUND,
  MODE_BREATHING,
  MODE_COUNT_DOWN,
  MODE_LETTERS,
  MODE_STARRY_NIGHT
};

DisplayMode currentMode = MODE_GREEN_BACKGROUND;
unsigned long lastButtonPress = 0;
const unsigned long debounceDelay = 50;

void setup() {
  FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
  FastLED.setBrightness(60);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
}

void loop() {
  // 检测按钮按下切换模式
  if (digitalRead(BUTTON_PIN) == LOW && (millis() - lastButtonPress > debounceDelay)) {
    lastButtonPress = millis();
    currentMode = static_cast<DisplayMode>((currentMode + 1) % 7);
    delay(200); // 防止误触
  }

  // 根据当前模式执行对应显示
  switch (currentMode) {
    case MODE_GREEN_BACKGROUND:
      solidColor(CRGB::Green);
      break;
    case MODE_RED_BACKGROUND:
      solidColor(CRGB::Red);
      break;
    case MODE_BLUE_BACKGROUND:
      solidColor(CRGB::Blue);
      break;
    case MODE_BREATHING:
      breathingEffect();
      break;
    case MODE_COUNT_DOWN:
      countDown();
      break;
    case MODE_LETTERS:
      displayLetters();
      break;
    case MODE_STARRY_NIGHT:
      starryNight();
      break;
  }
}

// 纯色背景显示
void solidColor(CRGB color) {
  fill_solid(leds, NUM_LEDS, color);
  FastLED.show();
  delay(100);
}

// 呼吸灯效果
void breathingEffect() {
  static CRGB colors[3] = {CRGB::Green, CRGB::Red, CRGB::Blue};
  static int currentColor = 0;
  static uint8_t brightness = 0;
  static bool increasing = true;

  // 切换颜色
  if (brightness == 0 && !increasing) {
    currentColor = (currentColor + 1) % 3;
    increasing = true;
  }

  // 调整亮度
  if (increasing) {
    brightness += 2;
    if (brightness >= 255) {
      brightness = 255;
      increasing = false;
    }
  } else {
    brightness -= 2;
    if (brightness <= 0) {
      brightness = 0;
    }
  }

  fill_solid(leds, NUM_LEDS, colors[currentColor]);
  FastLED.setBrightness(brightness);
  FastLED.show();
  delay(20);
}

// 从9到0的数字倒计时
void countDown() {
  for (int i = 9; i >= 0; i--) {
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    displayDigit(i, CRGB::White);
    FastLED.show();
    delay(1000);
  }
  // 倒计时结束后等待一段时间
  delay(1000);
}

// 显示数字
void displayDigit(int digit, CRGB color) {
  for (int y = 0; y < 8; y++) {
    for (int x = 0; x < 5; x++) {
      if ((digits[digit][y] >> (4 - x)) & 1) {
        int index = y * 8 + x + 1; // +1使数字居中
        if (index < NUM_LEDS) {
          leds[index] = color;
        }
      }
    }
  }
}

// 从A到G的字母显示
void displayLetters() {
  for (int i = 0; i < 7; i++) {
    fill_solid(leds, NUM_LEDS, CRGB::Black);
    displayLetter(i, CRGB::Yellow);
    FastLED.show();
    delay(1000);
  }
  // 显示结束后等待一段时间
  delay(1000);
}

// 显示字母
void displayLetter(int letterIndex, CRGB color) {
  for (int y = 0; y < 8; y++) {
    for (int x = 0; x < 5; x++) {
      if ((letters[letterIndex][y] >> (4 - x)) & 1) {
        int index = y * 8 + x + 1; // +1使字母居中
        if (index < NUM_LEDS) {
          leds[index] = color;
        }
      }
    }
  }
}

// 星空效果（深蓝色背景+随机闪烁的星星）
void starryNight() {
  static const CRGB nightColor = CRGB(0, 0, 64); // 深蓝色
  static uint8_t starBrightness[NUM_LEDS] = {0};
  static unsigned long lastUpdate = 0;

  // 每30ms更新一次
  if (millis() - lastUpdate > 30) {
    lastUpdate = millis();
    
    // 背景为深蓝色
    fill_solid(leds, NUM_LEDS, nightColor);
    
    // 随机生成或熄灭星星
    for (int i = 0; i < NUM_LEDS; i++) {
      if (starBrightness[i] > 0) {
        // 星星逐渐熄灭
        starBrightness[i] -= 5;
        if (starBrightness[i] < 0) starBrightness[i] = 0;
        leds[i] = CRGB(starBrightness[i], starBrightness[i], starBrightness[i]);
      } else if (random(100) < 2) {
        // 随机生成新星星
        starBrightness[i] = random(100, 255);
        leds[i] = CRGB(starBrightness[i], starBrightness[i], starBrightness[i]);
      }
    }
    
    FastLED.show();
  }
}    